iT邦幫忙

2025 iThome 鐵人賽

DAY 24
0
DevOps

n8n x AI自動化之新手村系列 第 24

Day24 運動打卡+激勵機制 (進階版)

  • 分享至 

  • xImage
  •  

在昨天的運動打卡實作中,提到因為無法正確無法正確篩選Notion中的運動日期欄位,而沒辦法抓取到當天的運動紀錄,進而導致無法順利判斷運動狀態。
經過這次繼續嘗試,發現到是因為Date&Time節點的輸出通常是 ISO 8601 (YYYY-MM-DD 或帶時間的格式),而Notion日期是 YYYY/MM/DD 格式,因此後續在取得當日任務時會出現問題,所以需要先使用code轉成統一日期格式,並統一時區與取消帶時間的格式,只存取 YYYY/MM/DD的日期。

實作流程

這次做的是當使用者輸入今天的運動後會自動輸入到Notion中,每天晚上七點會檢查當日是否有運動,有的話傳送讚美的訊息,沒有的話傳送鼓勵提醒運動的訊息。因此會有兩個工作流。

一、輸入運動紀錄到Notion
這部分與昨天內容大致相同就不多做說明
https://ithelp.ithome.com.tw/upload/images/20250908/20168759C0UsFJIixY.png

二、檢查當日是否有運動並傳送對應的訊息
https://ithelp.ithome.com.tw/upload/images/20250908/20168759r0N8AUIvR0.png

  1. 建立schedule trigger節點

    1. 設定每天晚上七點觸發
  2. 建立Date&Time節點,取得當日的日期

    1. Timezone設為Asia/Taipei
      https://ithelp.ithome.com.tw/upload/images/20250908/20168759OFd1333aoE.png
  3. 建立code節點,編輯date的格式為yyyy/mm/dd(與notion格式相同)

    const dateString =$('取得當天日期1').first().json.currentDate ;
    const tz = "Asia/Taipei";
    const d = new Date(dateString);
    
    // 取得 YYYY/MM/DD
    const yyyy = d.toLocaleString('en-US', { timeZone: tz, year: 'numeric' });
    const mm = d.toLocaleString('en-US', { timeZone: tz, month: '2-digit' });
    const dd = d.toLocaleString('en-US', { timeZone: tz, day: '2-digit' });
    const formatted = `${yyyy}/${mm}/${dd}`;
    return [{ json: { formattedDate: formatted } }];
    
  4. 建立notion節點,功能為gat database page

    1. 設定要的database
    2. filter: 運動日期等於當天日期
    3. 在settings中【Always Output Data】開啟,確保沒有抓到運動紀錄時也能輸出item
      https://ithelp.ithome.com.tw/upload/images/20250908/20168759WNB9naPTfV.png
  5. 建立IF節點,來分支「有運動」和「沒運動」的路徑,並各自加上code節點來編輯訊息

    1. 「有運動」→ 讚美,會隨機從五個語句中選出一個傳送

      // 有運動的情況
      const messages = [ 
        "太棒了!今天完成了運動 💪", 
        "持續努力超讚 👏", 
        "你今天的堅持讓人佩服 🔥", 
        "今日運動完成!好習慣正在養成 ✨", 
        "今日運動完成!堅持就是勝利 🔥" 
      ]; 
      // 隨機挑一句 
      const message = messages[Math.floor(Math.random() * messages.length)]; 
      return [{ json: { message } }];
      
    2. 「沒運動」→ 鼓勵提醒,會隨機從四個語句中選出一個傳送

      // 沒有運動的情況
      const messages = [
        "今天還沒有運動喔,加油!動起來 💡",
        "再小的運動也是進步,趕快動一下吧 ⚡",
        "記得流點汗,給自己一點能量 💦",
        "別忘了運動,保持好狀態 🔥"
      ];
      
      // 隨機挑一句
      const message = messages[Math.floor(Math.random() * messages.length)];
      
      return [{ json: { message } }];
      
      
  6. 建立telegram節點,功能為send a message

成果(以9/8為例)

  • Notion
    https://ithelp.ithome.com.tw/upload/images/20250908/20168759w3Sd3JlGfZ.jpg
  • 9/8有一筆在家瑜珈40分鐘的運動紀錄時
    https://ithelp.ithome.com.tw/upload/images/20250908/20168759AnFVXyBuSd.png
  • 刪掉9/8運動紀錄時
    https://ithelp.ithome.com.tw/upload/images/20250908/20168759SoQjnWFsMM.png

上一篇
Day23 運動打卡
下一篇
Day 25 每日推播多益單字
系列文
n8n x AI自動化之新手村28
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言